<template>
  <el-table
    :data="PermissionArr"
    style="width: 100%; margin-bottom: 20px"
    row-key="id"
    border
  >
    <el-table-column
      v-model="MenuData.name"
      label="名称"
      prop="name"
    ></el-table-column>
    <el-table-column
      v-model="MenuData.code"
      label="权限值"
      prop="code"
    ></el-table-column>
    <el-table-column label="修改时间" prop="updateTime"></el-table-column>
    <el-table-column label="操作">
      <template #="{ row, $index }">
        <el-button
          type="primary"
          @click="addPermission(row)"
          size="default"
          :disabled="row.level == 4 ? true : false"
        >
          {{ row.level == 3 ? '添加功能' : '添加菜单' }}
        </el-button>
        <el-button
          type="primary"
          @click="updatePermission(row)"
          size="default"
          :disabled="row.level == 1 ? true : false"
        >
          编辑
        </el-button>
        <el-popconfirm
          :title="`你确定删除${row.name}吗？`"
          @confirm="removeMenu(row)"
        >
          <el-button
            type="primary"
            size="default"
            :disabled="row.level == 1 ? true : false"
          >
            删除
          </el-button>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <!-- 添加或修改已有菜单使用对话框 -->
  <el-dialog
    :title="MenuData.id ? '编辑菜单' : '添加菜单'"
    v-model="dialogVisible"
    width="30%"
  >
    <el-form>
      <!-- label-width -->
      <el-form-item label="名称" label-width="60px">
        <el-input v-model="MenuData.name" placeholder="请填写名称"></el-input>
      </el-form-item>
      <el-form-item label="权限值" label-width="60px">
        <el-input v-model="MenuData.code" placeholder="请填权限值"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" size="default" @click="cancel">
          取消
        </el-button>
        <el-button type="primary" size="default" @click="confirm">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
  <!-- 测试button -->
  <el-button type="primary" @click="addPermission1" size="default">
    添加菜单
  </el-button>
  <el-button type="primary" @click="updatePermission1" size="default">
    编辑
  </el-button>
  <el-popconfirm :title="`你确定删除此条菜单吗？`" @confirm="removeMenu">
    <template #reference>
      <el-button type="primary" size="default">删除</el-button>
    </template>
  </el-popconfirm>
</template>

<script lang="ts" setup>
import {
  reqAddOrupdateMenu,
  reqAllPermission,
  reqRemoveMenu,
} from '@/api/acl/menu'
import type {
  MenuParams,
  Permission,
  PermissionResponseData,
} from '@/api/acl/menu/type'
import { ElMessage } from 'element-plus'
import { el, id } from 'element-plus/es/locales.mjs'
import { ref, onMounted, reactive } from 'vue'
let dialogVisible = ref<boolean>(false)
//存储全部菜单数据
let PermissionArr = ref<Permission>()
//收集新增和修改菜单请求的参数
let MenuData = reactive<MenuParams>({
  id: 0,
  code: '', //权限值
  level: 0,
  name: '',
  pid: 0, //菜单的id
})
onMounted(() => {
  getAllPermission()
})
//获取全部菜单数据方法
async function getAllPermission() {
  let result: any =
    (await reqAllPermission) as unknown as PermissionResponseData
  if (result.code === 200) {
    PermissionArr.value = result.data
  }
}
//添加菜单按钮回调
function addPermission(row: Permission) {
  //清空数据
  Object.assign(MenuData, {
    id: 0,
    code: '', //权限值
    level: 0,
    name: '',
    pid: 0, //菜单的id
  })
  dialogVisible.value = true
  //收集新增菜单的level值，新增菜单新增的是子菜单
  MenuData.level = row.level + 1
  //收集新增菜单的id，给谁新增子菜单
  MenuData.pid = row.id as number
}
function addPermission1() {
  Object.assign(MenuData, {
    id: 0,
    code: '', //权限值
    level: 0,
    name: '',
    pid: 0, //菜单的id
  })
  dialogVisible.value = true
}
//编辑菜单按钮回调
function updatePermission(row: Permission) {
  dialogVisible.value = true
  Object.assign(MenuData, row)
}
function updatePermission1(id = 1) {
  dialogVisible.value = true
  MenuData.id = id
}
//对话框确定按钮的回调、
async function confirm() {
  let result: any = await reqAddOrupdateMenu(MenuData)
  if (result.code === 200) {
    dialogVisible.value = false
    ElMessage({
      type: 'success',
      message: MenuData.id ? '修改成功' : '添加成功',
    })
  } else {
    ElMessage({
      type: 'error',
      message: MenuData.id ? '修改失败' : '添加失败',
    })
  }
  getAllPermission()
}
//取消按钮的回调
function cancel() {
  dialogVisible.value = false
}
//删除气泡确认框的回调
async function removeMenu(row: Permission) {
  let result: any = await reqRemoveMenu(row.id as number)
  if (result.code === 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
  }
}
</script>

<style scoped lang="scss"></style>
